package com.zp.datax.core;

import com.zp.datax.core.function.ConsumerWrapper;
import com.zp.datax.core.model.DataxJobLog;
import com.zp.datax.core.result.ReturnT;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;

/**
 * 线程自动调用脚本处理器
 *
 * @author bruce
 * @date 2019-07-15
 */
@Slf4j
public class ThreadExecProcessor extends ExecProcessor {

    //条件器
    private Condition condition;

    public ThreadExecProcessor(String pathname
            , String strCondition) {
        super(pathname, strCondition);
    }

    public ThreadExecProcessor(ExecProcessor processor) {
        super(processor);
    }

    public ThreadExecProcessor(String pathname
            , String strCondition
            , Condition condition) {
        this(pathname, strCondition);
        this.condition = condition;
    }

    public ThreadExecProcessor(ExecProcessor processor, Condition condition) {
        this(processor);
        this.condition = condition;
    }


    /**
     * @param handler
     * @param consumerWrapper
     * @param dataxJobLog
     * @return
     * @throws IllegalArgumentException
     */
    @Override
    public ReturnT exec(Handler handler, ConsumerWrapper<DataxJobLog, Exception> consumerWrapper, DataxJobLog dataxJobLog) {

        Object[] objarray = condition.append(strCondition);//参数组合可能会有异常

        String realCondition = Objects.toString(objarray[0]);
        //去掉最后","
        if (realCondition.contains(",")) {
            realCondition = realCondition.substring(0, realCondition.length() - 1);
        }
        processor.setStrCondition(realCondition);

        if (Objects.nonNull(dataxJobLog)) {
            dataxJobLog.setRealParams(realCondition);
        }

        ReturnT returnT = processor.exec(handler, consumerWrapper, dataxJobLog);

        log.info("taskId :: {}, 执行结果 :: {}", dataxJobLog.getTaskId(), returnT.getCode());

        return returnT;
    }
}
